package com.qf.service.impl;

import com.qf.entity.Classroom;
import com.qf.entity.Student;
import com.qf.entity.StudentVO;
import com.qf.entity.Subject;
import com.qf.mapper.ClassroomMapper;
import com.qf.mapper.StudentMapper;
import com.qf.mapper.SubjectMapper;
import com.qf.service.StudentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author cuicui
 * @Description
 * @Date 2025-8-15
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassroomMapper classroomMapper;
    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public Student findById(int id) {
        Student student = studentMapper.findById(id);
        return student;
    }

    @Override
    public List<Student> findAllStudent() {
        List<Student> list = studentMapper.findAllStudent();
        return list;
    }

    @Override
    public List<Student> findStudentByAgeOrName(String name, Integer age) {
        List<Student> list = studentMapper.findStudentByAgeOrName(name, age);
        return list;
    }

    @Override
    public int addStudent(Student student) {
        int num = studentMapper.addStudent(student);
        return num;
    }

    @Override
    public int updateStudent(Student student) {
        int num = studentMapper.updateStudent(student);
        return num;
    }

    @Override
    public int deleteStudent(int id) {
        int num = studentMapper.deleteStudent(id);
        return num;
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        studentMapper.deleteBatch(ids);
    }

    @Override
    public List<StudentVO> findAllStudentVO() {
        return studentMapper.findAllStudentAndClassroom();
    }

    @Override
    public StudentVO findStudentAndAllSubjectByStuid(int sid) {
        return studentMapper.findStudentAndAllSubjectByStuid(sid);
    }

    @Override
    public StudentVO findStudentMsgByStuid(int sid) {
        return studentMapper.findStudentMsgByStuid(sid);
    }

    @Override
    public List<StudentVO> findAllStudentAndClassroomV2() {
        List<StudentVO> list = new ArrayList<>( );
        // 找到所有学生
        List<Student> allStudent = studentMapper.findAllStudent();
        // 遍历所有学生
        allStudent.forEach(
                student -> {
                    // 再根据学生对象中的教室id查
                    Classroom classroom = classroomMapper.getMsgById(student.getId());
                    // 创建最终封装数据的对象
                    StudentVO studentVO = new StudentVO( );
                    // BeanUtils是spring框架提供的工具类,copyProperties方法用与拷贝对象属性
                    // 此处是将student对象中的属性赋值到studentVO中
                    BeanUtils.copyProperties(student, studentVO);
                    // studentVO中存储教室信息
                    studentVO.setClassroom(classroom);
                    // 将每一个StudentVO添加到List集合
                    list.add(studentVO);
                    System.out.println(111111111);
                    System.out.println(list);
                }
        );
        return list;
    }

    @Override
    public StudentVO getSubjectById(int sid) {
        // 先根据学生id查出1个学生
        Student student = studentMapper.findById(sid);
        // 创建集合准备存储每个学科对象
        List<Subject> subjectList = new ArrayList<>(  );
        // 再根据学生id查出关联表中所关联的所有学科id
        List<Integer> subIdList = studentMapper.findAllSubjectId(sid);
        // 遍历所有id
        subIdList.forEach(subId -> {
            // 再根据学科id获得每一个学科对象
            Subject subject = subjectMapper.findSubjectById(subId);
            // 存储学科对象
            subjectList.add(subject);
        });
        StudentVO studentVO = new StudentVO( );
        BeanUtils.copyProperties(student,studentVO);
        studentVO.setSubjectListV2(subjectList);
        return studentVO;
    }

    @Override
    public List<Integer> findAllSubjectId(int sid) {
        return studentMapper.findAllSubjectId(sid);
    }

}
